package com.aygxy.carproject.service.impl;

import com.aygxy.carproject.ex.ServiceCode;
import com.aygxy.carproject.ex.ServiceException;
import com.aygxy.carproject.mapper.CategoryMapper;
import com.aygxy.carproject.mapper.ProductSkuMapper;
import com.aygxy.carproject.mapper.ProductSpuMapper;
import com.aygxy.carproject.pojo.category.vo.CategoryStandardVO;
import com.aygxy.carproject.pojo.product.dto.SpuAddNewDTO;
import com.aygxy.carproject.pojo.product.dto.SpuUpdateDTO;
import com.aygxy.carproject.pojo.product.entity.Spu;
import com.aygxy.carproject.pojo.product.entity.SpuDetail;
import com.aygxy.carproject.pojo.product.entity.SpuEntity;
import com.aygxy.carproject.pojo.product.entity.SpuQuery;
import com.aygxy.carproject.pojo.product.vo.SpuDetailStandardVO;
import com.aygxy.carproject.pojo.product.vo.SpuListItemVO;
import com.aygxy.carproject.pojo.product.vo.SpuStandardVO;
import com.aygxy.carproject.service.ProductSpuService;
import com.aygxy.carproject.utils.IdGeneratorUtils;
import com.aygxy.carproject.web.JsonPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ProductSpuServiceImpl implements ProductSpuService {
    @Autowired
    private ProductSpuMapper productSpuMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public JsonPage<SpuEntity> getSpuByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SpuEntity> list=productSpuMapper.findAllList();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public JsonPage<SpuListItemVO> listSpuByCategoryId(Long categoryId, Integer page, Integer pageSize) {
        SpuQuery spuQuery=new SpuQuery();
        spuQuery.setCategoryId(categoryId);
        PageHelper.startPage(page,pageSize);
        List<SpuListItemVO> spuListItemVOs = productSpuMapper.listByCustomCondition(spuQuery);
        return JsonPage.restPage(new PageInfo<>(spuListItemVOs));
    }

    @Override
    public SpuStandardVO getSpuById(Long id) {
        SpuStandardVO spuStandardVO = productSpuMapper.getById(id);
        if (spuStandardVO == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "查询SPU详情失败，尝试访问的SPU数据不存在！");
        }
        return spuStandardVO;
    }

    @Override
    public void addNew(SpuAddNewDTO spuAddNewDTO) {

        // 获取类别ID
        Long categoryId = spuAddNewDTO.getCategoryId();
        // 查询类别信息
        CategoryStandardVO category = categoryMapper.getById(categoryId);
        // 检查类别是否存在
        if (category == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "新增SPU失败，选择的【类别】不存在！");
        }
        // 检查类别是否启用
        if (category.getEnable() == null || category.getEnable() == 0) {
            throw new ServiceException(ServiceCode.NOT_ACCEPTABLE, "新增SPU失败，选择的【类别】已禁用！");
        }
        // 检查类别数据是否完整
        if (category.getParent() == null) {
            throw new ServiceException(ServiceCode.NOT_ACCEPTABLE, "新增SPU失败，选择的【类别】数据不完整，请联系系统管理员！");
        }
        // 检查所提交的类别是否包含子级（不允许在父级类别中创建SPU）
        if (category.getParent().equals(1)) {
            throw new ServiceException(ServiceCode.NOT_ACCEPTABLE, "新增SPU失败，选择的【类别】仍包括子级！");
        }

        // 准备SPU数据
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuAddNewDTO, spu);
        spu.setCategoryName(category.getName());
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        spu.setDeleted(0);
        spu.setNewArrival(0);
        spu.setRecommend(0);
        spu.setChecked(0);
        spu.setCheckUser(null);
        spu.setGmtCheck(null);
        // 执行插入
        int rows = productSpuMapper.insert(spu);
        if (rows != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "新增SPU失败，服务器忙，请稍后再次尝试！");
        }

//        // 如果还提交了详情，则插入详情
//        if (spuAddNewDTO.getDetail() != null) {
//            SpuDetail spuDetail = new SpuDetail();
//            spuDetail.setSpuId(spuId);
//            spuDetail.setDetail(spuAddNewDTO.getDetail());
//            rows = spuDetailMapper.insert(spuDetail);
//            if (rows != 1) {
//                throw new ServiceException(ServiceCode.ERR_INSERT, "新增SPU失败，服务器忙，请稍后再次尝试！");
//            }
//        }
    }

    @Override
    public void updateById(Long id, SpuUpdateDTO spuDTO) {
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuDTO, spu);
//        String detail = spuDTO.getDetail();
//        if (detail != null && detail.trim().length() > 0) {
//            spuDetailMapper.updateDetailBySpuId(spu.getId(), spuDTO.getDetail());
//        }
        productSpuMapper.update(spu);
    }

}
